package com.example.asproject1801_1.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import java.util.Calendar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class ClockView extends View {
    Paint paint,hpaint;
    int hour,minute,seconds;  //时分秒

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            getTime();
            invalidate();
            handler.sendEmptyMessageDelayed(1,1000);
        }
    };
    public ClockView(Context context) {
        super(context);
        initPaint();
    }

    public ClockView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
        getTime();
        handler.sendEmptyMessageDelayed(1,1000);
    }
    //    获取当前的时间
    public void getTime(){
        Calendar calendar = Calendar.getInstance();
        hour = calendar.get(Calendar.HOUR);
        minute = calendar.get(Calendar.MINUTE);
        seconds = calendar.get(Calendar.SECOND);
    }
    public void initPaint(){
        paint = new Paint();
        paint.setTextSize(20);
        paint.setAntiAlias(true); //设置抗锯齿

        hpaint = new Paint();
        hpaint.setTextSize(20);
        hpaint.setAntiAlias(true); //设置抗锯齿
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.BLACK);
//        绘制表盘
        paint.setStrokeWidth(5);  //设置线条的粗细
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2-10,paint);
//        绘制内圈的表盘
        paint.setStrokeWidth(4);
        canvas.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2-20,paint);
//        绘制刻度线
//        通过旋转画布，绘制刻度
        hpaint.setColor(Color.RED);
        for (int i = 0; i < 12; i++) {
//            保存画布旋转之前的状态
            canvas.save();
            canvas.rotate(360/12*(i+1),getWidth()/2,getHeight()/2);
            canvas.drawLine(getWidth()/2,30,getWidth()/2,40,hpaint);
            canvas.drawText(String.valueOf(i+1),getWidth()/2-5,60,hpaint);
            //            恢复画布
            canvas.restore();
        }

//        绘制时针
        canvas.save();
        canvas.rotate(hour*30+0.5f*minute,getWidth()/2,getHeight()/2);
        hpaint.setStrokeWidth(10);
        canvas.drawLine(getWidth()/2,getHeight()/2+getHeight()/10,
                getWidth()/2,getHeight()/2-getHeight()/5,hpaint);
        canvas.restore();

//        绘制分针和秒针
        paint.setColor(Color.BLACK);
        canvas.save();
        paint.setStrokeWidth(6);
        canvas.rotate(6*minute,getWidth()/2,getHeight()/2);
        canvas.drawLine(getWidth()/2,getHeight()/2+getHeight()/10,
                getWidth()/2,getHeight()/2-getHeight()/4,paint);
        canvas.restore();

//        绘制秒针
        canvas.save();
        paint.setStrokeWidth(3);
        canvas.rotate(seconds*6,getWidth()/2,getHeight()/2);
        canvas.drawLine(getWidth()/2,getHeight()/2+getHeight()/10,
                getWidth()/2,getHeight()/2-getHeight()/3,paint);
        canvas.restore();

        //绘制表的轴心
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(getWidth()/2,getHeight()/2,10,paint);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        获取模式和最大尺寸
        int wmode = MeasureSpec.getMode(widthMeasureSpec);
        int hmode = MeasureSpec.getMode(heightMeasureSpec);
        int wsize = MeasureSpec.getSize(widthMeasureSpec);
        int hsize = MeasureSpec.getSize(heightMeasureSpec);
//        画圆形，按照宽度和高度小的那个画半径
        switch (wmode) {
            case MeasureSpec.EXACTLY:
                wsize = hsize = Math.min(wsize,hsize);
                break;
            case MeasureSpec.AT_MOST:
                if (hmode == MeasureSpec.AT_MOST) {
                    wsize = hsize = 300;
                }else {
                    wsize = hsize;
                }
                break;
        }
//        设置显示宽高
        setMeasuredDimension(wsize,hsize);

    }
}